package com.android.gufengmall.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.GoodsBean;
import com.android.gufengmall.bean.NormalBean;
import com.android.gufengmall.bean.OrderBean;
import com.android.gufengmall.cart.viewholder.ChildViewHolder;
import com.android.gufengmall.cart.viewholder.GroupViewHolder;
import com.android.gufengmall.cart.viewholder.NormalViewHolder;
import com.ocnyang.cartlayout.CartAdapter;
import com.ocnyang.cartlayout.viewholder.CartViewHolder;

import java.util.List;

public class ShopCartAdapter extends CartAdapter<CartViewHolder> {

    private RemoveDataCallBack mRemoveDataCallBack;

    public ShopCartAdapter(Context context, List datas) {
        super(context, datas);
    }

    @Override
    protected CartViewHolder getNormalViewHolder(View itemView) {
        return new NormalViewHolder(itemView, -1);
    }

    @Override
    protected CartViewHolder getGroupViewHolder(View itemView) {
        return (CartViewHolder) new GroupViewHolder(itemView, R.id.checkbox);
    }

    @Override
    protected CartViewHolder getChildViewHolder(View itemView) {
        return (CartViewHolder) (new ChildViewHolder(itemView, R.id.checkbox) {
            @Override
            public void onNeedCalculate() {
                if (onCheckChangeListener != null) {
                    onCheckChangeListener.onCalculateChanged(null);
                }
            }
        });
    }

    @Override
    protected int getChildItemLayout() {
        return R.layout.item_cart_child;
    }

    @Override
    protected int getGroupItemLayout() {
        return R.layout.item_cart_group;
    }

    @Override
    protected int getNormalItemLayout() {
        return R.layout.item_cart_normal;
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        if (holder instanceof ChildViewHolder) {
            ChildViewHolder childViewHolder = (ChildViewHolder) holder;
            childViewHolder.textView.setText(((OrderBean) mDatas.get(position)).getOrderName());
            childViewHolder.textType.setText(((OrderBean) mDatas.get(position)).getOrderInfo());
            childViewHolder.textViewPrice.setText(
                    mContext.getString(R.string.rmb_X, ((OrderBean) mDatas.get(position)).getOrderPrice()));
            childViewHolder.textViewNum.setText(String.valueOf(((OrderBean) mDatas.get(position)).getOrderAmount()));
            childViewHolder.imgShopCart.setImageBitmap(BitmapFactory.decodeFile(((OrderBean) mDatas.get(position)).getOrderImgShop()));
        }else if (holder instanceof NormalViewHolder) {
            NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
            if (mDatas.size() <= 1) {
                normalViewHolder.itemView.setVisibility(View.GONE);
            } else {
                normalViewHolder.imgViewClose.setOnClickListener(v -> {
                    normalViewHolder.llContent.setVisibility(View.GONE);
                });
                normalViewHolder.textView.setText(mContext.getString(R.string.normal_tip_X,
                        ((NormalBean) mDatas.get(position)).getMarkdownNumber()));
            }
        }
    }

    public void setRemoveDataCallBack(RemoveDataCallBack removeDataCallBack) {
        mRemoveDataCallBack = removeDataCallBack;
    }

    public interface RemoveDataCallBack{
        void remoData(int position);
    }
}
